package com.study.demo01.web;

import cn.hutool.core.util.RandomUtil;
import com.study.demo01.config.ConfirmConfig;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <a href="https://www.bilibili.com/video/BV1cb4y1o7zz?p=72">发布确认</a>
 */
@Slf4j
@RestController
@RequestMapping("/confirm")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProducerController {
    private final RabbitTemplate rabbitTemplate;


    @GetMapping("/sendMessage/{type}/{msg}")
    public void sendMsg(@PathVariable("msg") String msg,@PathVariable("type") String type) {
        AtomicInteger incr = new AtomicInteger(1);
        log.info("发送消息[{}]", msg);
        //CorrelationData correlationData = new CorrelationData(IdUtil.nanoId());

        String routingKey = type.length() > 2 ? ConfirmConfig.confirm_routing_key2 : ConfirmConfig.confirm_routing_key;
        msg = StringUtils.repeat(msg, 10);
        for (int i = 1; i <= 5000; i++) {
            String randomStringUpper = RandomUtil.randomStringUpper(10);
            String id = incr.getAndIncrement() + "";
            CorrelationData correlationData = new CorrelationData(id);
            //rabbitTemplate.convertAndSend(ConfirmConfig.confirm_exchange_name, ConfirmConfig.confirm_routing_key + "ss", msg, correlationData);
            //rabbitTemplate.convertAndSend(ConfirmConfig.confirm_exchange_name, ConfirmConfig.confirm_routing_key, new Person().setName(id + "-" + msg + randomStringUpper), correlationData);
            rabbitTemplate.convertAndSend(ConfirmConfig.confirm_exchange_name, routingKey, id + "-" + msg + randomStringUpper, correlationData);
        }

    }

    @Data
    @Accessors(chain = true)
    public static class Person implements Serializable {
        private String name;
    }

}
